package com.lyjStudy.train.member.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.jwt.JWTUtil;
import com.lyjStudy.train.common.exception.BusinessException;
import com.lyjStudy.train.common.exception.BusinessExceptionEnum;
import com.lyjStudy.train.common.util.JwtUtil;
import com.lyjStudy.train.common.util.SnowUtil;
import com.lyjStudy.train.member.domain.Member;
import com.lyjStudy.train.member.domain.MemberExample;
import com.lyjStudy.train.member.mapper.MemberMapper;
import com.lyjStudy.train.member.req.MemberLoginReq;
import com.lyjStudy.train.member.req.MemberRegisterReq;
import com.lyjStudy.train.member.req.MemberSendCodeReq;
import com.lyjStudy.train.member.resp.MemberLoginResp;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;




@Service
public class MemberService {

    private static final Logger LOG = LoggerFactory.getLogger(MemberService.class);

    @Resource
    private MemberMapper memberMapper;

    public int count(){
        return Math.toIntExact(memberMapper.countByExample(null));
    }

    public Long register(MemberRegisterReq req){
        String mobile = req.getMobile();
        Member memberDB = selectByMobile(mobile);
        if(ObjectUtil.isNotNull(memberDB)){
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_EXIST);
        }
        Member member = new Member();
        member.setMobile(mobile);
        member.setId(SnowUtil.getSnowflakeNextId());
        memberMapper.insert(member);
        return member.getId();
    }

    public void sendCode(MemberSendCodeReq req){
        String mobile = req.getMobile();
        Member memberDB = selectByMobile(mobile);
        if(ObjectUtil.isNotNull(memberDB)){
            LOG.info("手机号已存在，不插入记录。");
        }
        else{
            LOG.info("手机号不存在，插入一条记录。");
            Member member = new Member();
            member.setMobile(mobile);
            member.setId(SnowUtil.getSnowflakeNextId());
            memberMapper.insert(member);
        }

        //生成验证码
        String code = "8888";
        LOG.info("生成短信验证码：{}",code);
        // String code = RandomUtil.randomString(4);

        // 保存短信记录表：手机号，短信验证码，有效期，是否已使用，业务类型，发送时间，使用时间
        LOG.info("保存短信记录表");

        // 对接短信通道，发送短信
        LOG.info("对接短信通道");

    }

    public MemberLoginResp login(MemberLoginReq req){
        String mobile = req.getMobile();
        Member memberDB = selectByMobile(mobile);

        //如果手机号不存在
        if(ObjectUtil.isNull(memberDB)){
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_NOT_EXIST);
        }

        //验证码是否正确
        if(!"8888".equals(req.getCode())){
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_CODE_ERROR);
        }

        MemberLoginResp memberLoginResp = BeanUtil.copyProperties(memberDB, MemberLoginResp.class);
//        //未封装JWTUtil
//        Map<String, Object> map = BeanUtil.beanToMap(memberLoginResp);
//        String key = "lyj12306";
//        String token = JWTUtil.createToken(map, key.getBytes());
        //封装JWTUtil
        String token = JwtUtil.createToken(memberLoginResp.getId(), memberLoginResp.getMobile());
        memberLoginResp.setToken(token);
        return memberLoginResp;
    }

    private Member selectByMobile(String mobile) {
        MemberExample example = new MemberExample();
        example.createCriteria().andMobileEqualTo(mobile);
        List<Member> members = memberMapper.selectByExample(example);
        if(CollUtil.isNotEmpty(members)){
            return members.get(0);
        }
        return null;
    }

}
